package com.rankeiot.platform.inner;

import com.rankeiot.platform.domain.SlowSqlLog;

import lombok.AllArgsConstructor;
import org.sagacity.sqltoy.dao.SqlToyLazyDao;
import org.sagacity.sqltoy.model.OverTimeSql;
import org.sagacity.sqltoy.plugins.OverTimeSqlHandler;
import org.sagacity.sqltoy.utils.IdUtil;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

@AllArgsConstructor
public class DBOverTimeSqlHandler implements OverTimeSqlHandler {

    final SqlToyLazyDao dao;

    @Override
    public void log(OverTimeSql overTimeSql) {
       // System.out.println("logged over time sql");

        overTimeSql.setId(Long.toString(IdUtil.getNanoTimeId("").longValue(),Character.MAX_RADIX));
        //overTimeSql.getLogTime()
        SlowSqlLog log=new SlowSqlLog();

        log.setSql(overTimeSql.getSql());
        log.setTakeTime(overTimeSql.getTakeTime());
        log.setCodeTrace(overTimeSql.getCodeTrace());
        log.setOverTimeCount(overTimeSql.getOverTimeCount());
        log.setLogTime(asDate(overTimeSql.getLogTime()));
        dao.save(log);
    }
    public static Date asDate(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }
}
